package com.bdqn.service.impl;

import com.bdqn.mapper.BaseDao;
import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Objects;

public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 实现登录的功能
     *
     * @param account
     * @param password
     */
    @Override
    public SysUser login(String account, String password) {
        SysUser sysUser = null;
        try {
            sysUser = sysUserMapper.selectByAccount(account);
            if (sysUser != null) {
                if (!Objects.equals(sysUser.getPassword(), password)) {
                    sysUser = null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysUser;
    }

    /**
     * 根据角色名称、角色id查询总数据量
     *
     * @param realName
     * @param roleId
     * @return
     */
    @Override
    public int getUserCount(String realName, int roleId) {
        return sysUserMapper.getUserCount(realName, roleId);
    }

    /**
     * 分页查询
     *
     * @param realName
     * @param roleId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<SysUser> getUserList(String realName, int roleId, int pageIndex, int pageSize) {
        return sysUserMapper.getUserList(realName, roleId, pageIndex, pageSize);
    }

    @Override
    public int add(SysUser sysUser) {
        return 0;
    }

    @Override
    public int add(Connection connection, SysUser sysUser) {
        PreparedStatement pstm = null;
        int updateRows = 0;
        if (null != connection) {
            String sql = "insert into sys_user(account, realName,password, roleId, sex, birthday, phone, address, createdTime, createdUserId, idPicPath)" +
                    "values(?,?,?,?,?,?,?,?,?,?,?)";
            Object[] params = {sysUser.getAccount(), sysUser.getRealName(), sysUser.getPassword(), sysUser.getRoleId(), sysUser.getSex(), sysUser.getBirthday(), sysUser.getPhone(), sysUser.getAddress(), sysUser.getCreatedTime(), sysUser.getCreatedUserId(), sysUser.getPicPath()};
            updateRows = BaseDao.executeUpdate(connection, pstm, sql, params);
            BaseDao.classResource(null, pstm, null);
        }

        return updateRows;
    }

    @Override
    public SysUser getUserById(String id) {
        return null;
    }


    @Override
    public SysUser getUserById(Integer id) {
        return null;
    }

    @Override
    public boolean update(SysUser sysUser) {
        return false;
    }

}
